package com.smedi.ismedi.inmail.core.port.adapter.persistence.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.smedi.ismedi.authority.starter.util.VerifyHelper;
import com.smedi.ismedi.inmail.core.domain.aggregatemodel.ProcessVariable;
import com.smedi.ismedi.inmail.core.domain.dto.ProcessVariableQuery;
import com.smedi.ismedi.inmail.core.domain.repository.ProcessVariableRepository;
import com.smedi.ismedi.inmail.core.port.adapter.convert.AdapterConverter;
import com.smedi.ismedi.inmail.core.port.adapter.persistence.model.ProcessVariableEntity;
import com.smedi.ismedi.inmail.core.port.adapter.persistence.mybatis.mapper.ProcessVariableMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 主体院关系实现类
 *
 * @author niuyanling
 * @date 2021/08/05
 */
@Repository
@AllArgsConstructor
public class ProcessVariableRepositoryImpl implements ProcessVariableRepository {
    private final AdapterConverter adapterConverter;
    private final VerifyHelper verifyHelper;

    private final ProcessVariableMapper processVariableMapper;

    @Override
    public ProcessVariable getProcessVariableOne(ProcessVariableQuery processVariableQuery) {
        LambdaQueryWrapper<ProcessVariableEntity> lambdaQueryWrapper = this.ProcessVariableQueryToLambdaQueryWrapper(processVariableQuery);

        ProcessVariableEntity processVariableEntity = processVariableMapper.selectOne(lambdaQueryWrapper);

        return adapterConverter.processVariableEntityToProcessVariable(processVariableEntity);
    }

    @Override
    public List<ProcessVariable> getProcessVariableList(ProcessVariableQuery processVariableQuery) {
        LambdaQueryWrapper<ProcessVariableEntity> lambdaQueryWrapper = this.ProcessVariableQueryToLambdaQueryWrapper(processVariableQuery);

        List<ProcessVariableEntity> processVariableEntities = processVariableMapper.selectList(lambdaQueryWrapper);

        return adapterConverter.processVariableEntityToProcessVariable(processVariableEntities);
    }

    private LambdaQueryWrapper<ProcessVariableEntity> ProcessVariableQueryToLambdaQueryWrapper(ProcessVariableQuery processVariableQuery) {
        LambdaQueryWrapper<ProcessVariableEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.eq(verifyHelper.isAvailable(processVariableQuery.getId()), ProcessVariableEntity::getId, processVariableQuery.getId());
        lambdaQueryWrapper.eq(verifyHelper.isAvailable(processVariableQuery.getIds()), ProcessVariableEntity::getId, processVariableQuery.getIds());
        lambdaQueryWrapper.eq(verifyHelper.isAvailable(processVariableQuery.getTaskId()), ProcessVariableEntity::getTaskId, processVariableQuery.getTaskId());
        lambdaQueryWrapper.in(verifyHelper.isAvailable(processVariableQuery.getTaskIds()), ProcessVariableEntity::getTaskId, processVariableQuery.getTaskIds());

        return lambdaQueryWrapper;
    }
}